<?php
class ProcessCategory{

	
	/*
		Add a category
	*/
	function addCategory($name,$description,$image,$parentId)
	{
		$catImage = $this->uploadImage($image, $_SERVER['DOCUMENT_ROOT'] . '/znz/public/picture/category/');
		
		$sql   = "INSERT INTO tbl_category (cat_parent_id, cat_name, cat_description, cat_image) 
				  VALUES ($parentId, '$name', '$description', '$catImage')";
		$CI = get_instance();
		$query =$CI->db->query($sql);            
	}
	
	/*
		Upload an image and return the uploaded image name 
	*/
	function uploadImage($inputName, $uploadDir)
	{
		//$image     = $_FILES[$inputName];
		$image=$inputName;
		$imagePath = '';
		
		// if a file is given
		if (trim($image['tmp_name']) != '') {
			// get the image extension
			$ext = substr(strrchr($image['name'], "."), 1); 
	
			// generate a random new file name to avoid name conflict
			$imagePath = md5(rand() * time()) . ".$ext";
			
			// check the image width. if it exceed the maximum
			// width we must resize it
			$size = getimagesize($image['tmp_name']);
			//echo $size[0]."joy";
			if ($size[0] > 1024) {
				$imagePath = $this->createThumbnail($image['tmp_name'], $uploadDir . $imagePath, 1024);
			} else {
				// move the image to category image directory
				// if fail set $imagePath to empty string
				if (!move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath)) {
					$imagePath = '';
				}
			}	
		}
	
		
		return $imagePath;
	}
	
	/*
		Modify a category
	*/
	function modifyCategory($catId,$name,$description,$image)
	{

		$catImage = $this->uploadImage($image, $_SERVER['DOCUMENT_ROOT'] . '/znz/public/picture/category/');
	    //$catImage = $this->uploadImage($image, 'C:/xampp/htdocs/znz/public/picture/category/');

		// if uploading a new image
		// remove old image
		if ($catImage != '') {
			$this->_deleteImage($catId);
			$catImage = "'$catImage'";
		} else {
			// leave the category image as it was
			$catImage = 'cat_image';
		}
		$sql    = "UPDATE tbl_category 
				   SET cat_name = '$name', cat_description = '$description', cat_image = $catImage
				   WHERE cat_id = $catId";
		$CI = get_instance();
		$query =$CI->db->query($sql);
	}
	
	/*
		Remove a category
	*/
	function deleteCategory()
	{
		if (isset($_GET['catId']) && (int)$_GET['catId'] > 0) {
			$catId = (int)$_GET['catId'];
		} else {
			header('Location: index.php');
		}
		
		// find all the children categories
		$children = getChildren($catId);
		
		// make an array containing this category and all it's children
		$categories  = array_merge($children, array($catId));
		$numCategory = count($categories);
	
		// remove all product image & thumbnail 
		// if the product's category is in  $categories
		$sql = "SELECT pd_id, pd_image, pd_thumbnail
				FROM tbl_product
				WHERE cat_id IN (" . implode(',', $categories) . ")";
		$result = dbQuery($sql);
		
		while ($row = dbFetchAssoc($result)) {
			@unlink(SRV_ROOT . PRODUCT_IMAGE_DIR . $row['pd_image']);	
			@unlink(SRV_ROOT . PRODUCT_IMAGE_DIR . $row['pd_thumbnail']);
		}
		
		// delete the products
		$sql = "DELETE FROM tbl_product
				WHERE cat_id IN (" . implode(',', $categories) . ")";
		dbQuery($sql);
		
		// then remove the categories image
		_deleteImage($categories);
	
		// finally remove the category from database;
		$sql = "DELETE FROM tbl_category 
				WHERE cat_id IN (" . implode(',', $categories) . ")";
		dbQuery($sql);
		
		header('Location: index.php');
	}
	
	
	/*
		Recursively find all children of $catId
	*/
	function getChildren($catId)
	{
		$sql = "SELECT cat_id ".
			   "FROM tbl_category ".
			   "WHERE cat_parent_id = $catId ";
		$result = dbQuery($sql);
		
		$cat = array();
		if (dbNumRows($result) > 0) {
			while ($row = dbFetchRow($result)) {
				$cat[] = $row[0];
				
				// call this function again to find the children
				$cat  = array_merge($cat, getChildren($row[0]));
			}
		}
	
		return $cat;
	}
	
	
	/*
		Remove a category image
	*/
	function deleteImage()
	{
		if (isset($_GET['catId']) && (int)$_GET['catId'] > 0) {
			$catId = (int)$_GET['catId'];
		} else {
			header('Location: index.php');
		}
		
		_deleteImage($catId);
		
		// update the image name in the database
		$sql = "UPDATE tbl_category
				SET cat_image = ''
				WHERE cat_id = $catId";
		dbQuery($sql);        
	
		header("Location: index.php?view=modify&catId=$catId");
	}
	
	/*
		Delete a category image where category = $catId
	*/
	function _deleteImage($catId)
	{
		// we will return the status
		// whether the image deleted successfully
		$deleted = false;
	
		// get the image(s)
		$sql = "SELECT cat_image 
				FROM tbl_category
				WHERE cat_id ";
		
		if (is_array($catId)) {
			$sql .= " IN (" . implode(',', $catId) . ")";
		} else {
			$sql .= " = $catId";
		}	
	
		$CI = get_instance();
		$query =$CI->db->query($sql);
		//echo "joysaa";
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {
				// delete the image file
				$deleted = @unlink( 'C:/xampp/htdocs/znz/public/picture/category/'.$row->cat_image);
			}	
		}
		return $deleted;
	}

}
?>